local ClientOHOH = _G.ClientOHOH
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase())
local CallBackManager =  ClientOHOH.load.CallBackManager()
local ConnectClass = _G.CommonOHOH.load.Connect()
local FunctionList = ClientOHOH.load.FunctionList();
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local playerHeadshotList= {}
function M.new(...)
    return M(...)
end

function Initsts(ui)
    ui.insts = {}
    ui.insts.remainTime = "ctrl.time"
    ui.insts.headshot = "ctrl.headshot"
    ui.insts.folder = "ctrl.headshotFolder"
end


function M.ctor(self)
    Initsts(self)
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.PlayingGui"))
end


--更新玩家游戏进度,头像显示位置，进度文字等
function M.UpdatePlayerProgress(self)
    self:SetIconPosition()
end

--playerlist可以只有一个（用于监听），也可以是全部的正在游戏的玩家
function M.UpdateHeadshotPosition(self,playerList)
    for userId, player in pairs(playerList) do
        local positionY = 1-(player:GetAttribute("ActivatedRoadSum")/player:GetAttribute("RoadLeghtSum"))
        local tweenInfo =  TweenInfo.new(
            0.6,
            Enum.EasingStyle.Quart,
            Enum.EasingDirection.Out,
            0,
            false,
            0
        )
        local originPos = playerHeadshotList[userId].Position

        local target = {
            Position = UDim2.new(originPos.X.Scale,originPos.X.Offset,positionY,originPos.Y.Offset)
        }

        local tween = TweenService:Create(playerHeadshotList[userId],tweenInfo,target)
        tween:Play()
    end
end

--创建ui（初始化headshot）传入正在游玩玩家list，创建headshot到folder里
function M.InitHeadshots(self,playerList)
    for userId, _ in pairs(playerList) do
        local headshot = self.insts.headshot:Clone()
        headshot.Image = Players:GetUserThumbnailAsync(userId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size48x48)
        headshot.Name = userId
        headshot.Position = UDim2.new(0.5,0,1,0)
        headshot.Parent = self.insts.folder 
        headshot.Visible = true
        playerHeadshotList[userId] = headshot
    end
   
end



----------外部调用接口----------
--传入剩余游戏时间
function M.SetRemainTimeText(self,time)
    local second = time%60
    if time>=60 then
        local minute = math.floor(time/60)
        self.insts.remainTime.Text = minute..":"..string.format("%02d", second)
    else
        self.insts.remainTime.Text = second
    end
end

function M.ClearHeadshots(self)
    for _, value in pairs(playerHeadshotList) do
        value:Destroy()
    end
    playerHeadshotList = {}
end

function M.dtor(self)
    M.super.dtor(self)
end


return M